package com.simulation.superlotto.common.utils;

import com.simulation.superlotto.dao.RuleDao;
import com.simulation.superlotto.dao.WinnDao;
import com.simulation.superlotto.model.Rule;
import com.simulation.superlotto.model.Superlotto;
import com.simulation.superlotto.model.Winn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ：黄坤
 * @date ：创建于 2020/12/3
 * @description ：
 * @version: 1.0
 */
@Component
public class SuperlottoUtil {


    //获取当前期数
    //获取开奖时间

    public static WinnDao winnDao;

    public static RuleDao ruleDao;

    @Autowired
    public void setWinnDao(WinnDao winnDao) {
        SuperlottoUtil.winnDao = winnDao;
    }
    @Autowired
    public void setRuleDao(RuleDao ruleDao) {
        SuperlottoUtil.ruleDao = ruleDao;
    }


    /**
     *  获取归属期数和开奖日期
     *
     * @param superlotto
     * @return
     */
    public static Superlotto contextLoads(Superlotto superlotto) {
        String stringMonth= TimeUtils.getStringTimeDate(new Date());
        //获取最近日期参照
        Winn num = winnDao.selectMaxTime();
        //期数
        long num1 = num.getNum();
        //开奖时间
        String s = num.getTime().replaceAll("/", "-");
        //周几
        String week = getWeek(TimeUtils.getDate(s));
        //相差时间
        int i = TimeUtils.testBetweenDays2(s, stringMonth);
        int a = i > 7 ? i % 7 : i;
        int b = i > 7 ? (i / 7) : 0;
        Rule rule = ruleDao.selectRule(week, a);
        //期数
        long i2 = num1 + rule.getInterval() + b * 3;
        int i3 = b * 7 + rule.getDay();
        System.out.println("预计期数是" + i2);
        String date = TimeUtils.getDate(TimeUtils.getDate(s), i3);
        System.out.println("开奖时间:" + date);
        superlotto.setPeriodsNum(i2);
        superlotto.setLotteryTime(date);
        return superlotto;
    }

    public static String getWeek(Date date) {
        String[] weeks = {"星期日", "周一", "星期二", "周三", "星期四", "星期五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }


    public void sss(){

        List<Winn> winnList=  winnDao.selectTime();
        for (Winn winn : winnList) {
            String time = winn.getTime();
            String[] split = time.split("/");
            String s = split[1];
            String s1 = split[2];
            s=s.length()<2?"0"+s:s;
            s1=s1.length()<2?"0"+s1:s1;
            winn.setTime("2020/"+s+"/"+s1);
            int i = winnDao.updateById(winn);
        }
    }
    public void ddd(){
        List<Winn> winnList=  winnDao.selectTime();
        for (int i = 0; i < winnList.size()-1; i++) {
            Winn winn = winnList.get(i);
            String time = winn.getTime();
            String time1 = winnList.get(i + 1).getTime();

            int i1 = TimeUtils.testBetweenDays2(time.replaceAll("/", "-"), time1.replaceAll("/", "-"));
            System.out.println(i1);
            if (i1>3){
                System.out.println(winn.getTime());
            }

        }
    }

}
